
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.并行程序基础 · 计算机 ,java , 大数据 基础知识体系</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="hzh">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="basic/0.jhappenBefore与jvm指令重排.html" />
    
    
    <link rel="prev" href="basic/0.java内存模型与线程.html" />
    

    
    
    
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">操作系统</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../操作系统/1.基本概述.html">
            
                <a href="../操作系统/1.基本概述.html">
            
                    
                    1.操作系统基本概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../操作系统/2.进程管理之进程与线程.html">
            
                <a href="../操作系统/2.进程管理之进程与线程.html">
            
                    
                    2.进程管理之进程与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../操作系统/3.进程管理之处理机调度.html">
            
                <a href="../操作系统/3.进程管理之处理机调度.html">
            
                    
                    3.进程管理之处理机调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../操作系统/4.进程管理之进程同步.html">
            
                <a href="../操作系统/4.进程管理之进程同步.html">
            
                    
                    4.进程管理之进程同步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../操作系统/5.内存管理之基本概念.html">
            
                <a href="../操作系统/5.内存管理之基本概念.html">
            
                    
                    5.内存管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../操作系统/6.内存管理之虚拟内存.html">
            
                <a href="../操作系统/6.内存管理之虚拟内存.html">
            
                    
                    6.内存管理之虚拟内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../操作系统/7.IO设备管理之基本概念.html">
            
                <a href="../操作系统/7.IO设备管理之基本概念.html">
            
                    
                    7.IO设备管理之基本概念
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../操作系统/8.面试题总结一.html">
            
                <a href="../操作系统/8.面试题总结一.html">
            
                    
                    8.面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">计算机网络篇</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../计算机网络/1.计算机网络入门基础概念.html">
            
                <a href="../计算机网络/1.计算机网络入门基础概念.html">
            
                    
                    1.计算机网络入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../计算机网络/2.计算机网络通信基础.html">
            
                <a href="../计算机网络/2.计算机网络通信基础.html">
            
                    
                    2.计算机网络通信入门文章(必读)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../计算机网络/3.数据链路层.html">
            
                <a href="../计算机网络/3.数据链路层.html">
            
                    
                    3.数据链路层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../计算机网络/4.网络层1.html">
            
                <a href="../计算机网络/4.网络层1.html">
            
                    
                    4.网络层(一)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../计算机网络/5.网络层2.html">
            
                <a href="../计算机网络/5.网络层2.html">
            
                    
                    5.网络层(二)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../计算机网络/6.传输层.html">
            
                <a href="../计算机网络/6.传输层.html">
            
                    
                    6.传输层
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../计算机网络/7.应用层.html">
            
                <a href="../计算机网络/7.应用层.html">
            
                    
                    7.应用层(⭐)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../计算机网络/10.HTTP相关协议.html">
            
                <a href="../计算机网络/10.HTTP相关协议.html">
            
                    
                    8.HTTP相关及发展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" >
            
                <span>
            
                    
                    9.websocket
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../计算机网络/9.面试题二.html">
            
                <a href="../计算机网络/9.面试题二.html">
            
                    
                    面试题总结一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">数据结构篇</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../数据结构/1.图论.html">
            
                <a href="../数据结构/1.图论.html">
            
                    
                    1.图论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../数据结构/2.图论例题.html">
            
                <a href="../数据结构/2.图论例题.html">
            
                    
                    2.图论例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../数据结构/3.散列表.html">
            
                <a href="../数据结构/3.散列表.html">
            
                    
                    3.散列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../数据结构/4.排序算法.html">
            
                <a href="../数据结构/4.排序算法.html">
            
                    
                    4.排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="../数据结构/5.排序列题.html">
            
                <a href="../数据结构/5.排序列题.html">
            
                    
                    5.排序算法例题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="../数据结构/6.树论.html">
            
                <a href="../数据结构/6.树论.html">
            
                    
                    6.树论
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../数据结构/7.树论例题.html">
            
                <a href="../数据结构/7.树论例题.html">
            
                    
                    7.树论例题.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../数据结构/9.模板.html">
            
                <a href="../数据结构/9.模板.html">
            
                    
                    数据结构模板
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">算法篇</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../算法/leetCode周赛题解.html">
            
                <a href="../算法/leetCode周赛题解.html">
            
                    
                    leetCode周赛记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../算法/1.时间复杂度.html">
            
                <a href="../算法/1.时间复杂度.html">
            
                    
                    1.时间复杂度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../算法/2.深度优先搜索.html">
            
                <a href="../算法/2.深度优先搜索.html">
            
                    
                    2.深度优先搜索算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../算法/2.深度优先搜索题目集.html">
            
                <a href="../算法/2.深度优先搜索题目集.html">
            
                    
                    2.深搜题目集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../算法/3.广度优先搜索.html">
            
                <a href="../算法/3.广度优先搜索.html">
            
                    
                    3.广度优先搜索算法
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">JVM篇</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../jvm/1.jvm内存结构.html">
            
                <a href="../jvm/1.jvm内存结构.html">
            
                    
                    1.jvm内存结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../jvm/2.HotSpot虚拟机对象探秘.html">
            
                <a href="../jvm/2.HotSpot虚拟机对象探秘.html">
            
                    
                    2.HotSpot虚拟机对象探秘.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../jvm/3.对象存亡与垃圾收集算法.html">
            
                <a href="../jvm/3.对象存亡与垃圾收集算法.html">
            
                    
                    3.对象存亡与垃圾收集算法.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                <a href="../jvm/4.HotSpot的算法细节与垃圾收集器.html">
            
                    
                    4.HotSpot的算法细节与垃圾收集器.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../jvm/5.内存分配与回收策略.html">
            
                <a href="../jvm/5.内存分配与回收策略.html">
            
                    
                    5.内存分配与回收策略.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../jvm/6.类文件结构.html">
            
                <a href="../jvm/6.类文件结构.html">
            
                    
                    6.类文件结构.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="../jvm/7.类加载机制.html">
            
                <a href="../jvm/7.类加载机制.html">
            
                    
                    7.类加载机制.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="../jvm/8.jvm逃逸分析.html">
            
                <a href="../jvm/8.jvm逃逸分析.html">
            
                    
                    8.逃逸分析技术
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">多线程篇</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="basic/0.多线程路线图.html">
            
                <a href="basic/0.多线程路线图.html">
            
                    
                    1.多线程学习路线图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="basic/0.java内存模型与线程.html">
            
                <a href="basic/0.java内存模型与线程.html">
            
                    
                    2.java内存模型与线程
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="6.3" data-path="3.并行程序基础.html">
            
                <a href="3.并行程序基础.html">
            
                    
                    2.并行程序基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="basic/0.jhappenBefore与jvm指令重排.html">
            
                <a href="basic/0.jhappenBefore与jvm指令重排.html">
            
                    
                    3.happenBefore与jvm指令重排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="basic/1.synchronized与volatile.html">
            
                <a href="basic/1.synchronized与volatile.html">
            
                    
                    4.synchronized与volatile.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="basic/2.ThreadLocal.html">
            
                <a href="basic/2.ThreadLocal.html">
            
                    
                    5.ThreadLocal.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="cas/1.乐观锁与悲观锁.html">
            
                <a href="cas/1.乐观锁与悲观锁.html">
            
                    
                    6.乐观锁与悲观锁.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="cas/2.CAS与atomic类.html">
            
                <a href="cas/2.CAS与atomic类.html">
            
                    
                    7.CAS与atomic类.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.9" data-path="aqs/1.AQS.html">
            
                <a href="aqs/1.AQS.html">
            
                    
                    8.AQS.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.10" data-path="aqs/2.基于AQS的并发工具.html">
            
                <a href="aqs/2.基于AQS的并发工具.html">
            
                    
                    9.基于AQS的并发工具.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.11" data-path="blockingQueue/1.阻塞队列.html">
            
                <a href="blockingQueue/1.阻塞队列.html">
            
                    
                    10.阻塞队列.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.12" data-path="pond/1.线程池与底层原理.html">
            
                <a href="pond/1.线程池与底层原理.html">
            
                    
                    11.线程池与底层原理.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.13" data-path="10面试题一.html">
            
                <a href="10面试题一.html">
            
                    
                    12.面试题一
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.14" data-path="10面试题二.html">
            
                <a href="10面试题二.html">
            
                    
                    13.面试题二.md
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">java集合篇</li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../basic/collection/1.java集合.html">
            
                <a href="../basic/collection/1.java集合.html">
            
                    
                    1.集合架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../basic/collection/2.HashMap源码分析.html">
            
                <a href="../basic/collection/2.HashMap源码分析.html">
            
                    
                    2.hashMap底层原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="../basic/collection/3.ConcurrentHashMap分析.html">
            
                <a href="../basic/collection/3.ConcurrentHashMap分析.html">
            
                    
                    3.ConcurrentHashMap分析
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">设计模式篇</li>
        
        
    
        <li class="chapter " data-level="8.1" >
            
                <span>
            
                    
                    1.创造型模式
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="../设计模式/factory/三种工厂模式介绍.html">
            
                <a href="../设计模式/factory/三种工厂模式介绍.html">
            
                    
                    三种工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="../设计模式/factory/实战工厂模式.html">
            
                <a href="../设计模式/factory/实战工厂模式.html">
            
                    
                    实战工厂模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="../设计模式/factory/实战抽象工厂模式.html">
            
                <a href="../设计模式/factory/实战抽象工厂模式.html">
            
                    
                    实战抽象工厂模式
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">mysql篇</li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../数据库/mysql/1.b树b+树的底层原理.html">
            
                <a href="../数据库/mysql/1.b树b+树的底层原理.html">
            
                    
                    1.mysql索引基础之b树与b+树⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="../数据库/mysql/2.mysql索引.html">
            
                <a href="../数据库/mysql/2.mysql索引.html">
            
                    
                    2.mysql索引及优化⭐⭐⭐⭐
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="../数据库/mysql/3.mysql存储引擎.html">
            
                <a href="../数据库/mysql/3.mysql存储引擎.html">
            
                    
                    3.mysql存储引擎.md
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                <a href="../数据库/mysql/4.多版本并发控制之mvcc原理.html">
            
                    
                    4.多版本并发控制之mvcc原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="../数据库/mysql/4.mysql事务.html">
            
                <a href="../数据库/mysql/4.mysql事务.html">
            
                    
                    5.mysql事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.6" data-path="../数据库/mysql/5.mysql锁机制.html">
            
                <a href="../数据库/mysql/5.mysql锁机制.html">
            
                    
                    6.mysql的锁机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.7" data-path="../数据库/mysql/6.mysql面试题一.html">
            
                <a href="../数据库/mysql/6.mysql面试题一.html">
            
                    
                    7.mysql面试题一
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高并发中间件篇</li>
        
        
    

    
        
        <li class="header">架构理论篇</li>
        
        
    
        <li class="chapter " data-level="11.1" data-path="../架构/0.架构演进.html">
            
                <a href="../架构/0.架构演进.html">
            
                    
                    1.架构演进
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="11.2" data-path="../架构/0.微服务架构与soa架构.html">
            
                <a href="../架构/0.微服务架构与soa架构.html">
            
                    
                    2.微服务架构与soa架构
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高可用系统篇</li>
        
        
    
        <li class="chapter " data-level="12.1" data-path="../高可用架构/1.限流.html">
            
                <a href="../高可用架构/1.限流.html">
            
                    
                    1.系统限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="../高可用系统/2.服务熔断.html">
            
                <a href="../高可用系统/2.服务熔断.html">
            
                    
                    2.服务熔断
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="12.3" data-path="../高可用系统/3.服务降级.html">
            
                <a href="../高可用系统/3.服务降级.html">
            
                    
                    3.服务降级
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">spring-cloud 微服务篇</li>
        
        
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >2.并行程序基础</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;"><b></b>&#x4E8C;&#x3001;Java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;</a></li><ul><li><span class="title-icon "></span><a href="#1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;"><b></b>1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;</a></li><li><span class="title-icon "></span><a href="#2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;"><b></b>2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;</a></li><li><span class="title-icon "></span><a href="#3wait&#x548C;notify"><b></b>3wait()&#x548C;notify()</a></li><li><span class="title-icon "></span><a href="#4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;"><b></b>4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;</a></li><li><span class="title-icon "></span><a href="#5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;"><b></b>5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;</a></li><li><span class="title-icon "></span><a href="#6-volatile&#x5173;&#x952E;&#x5B57;"><b></b>6 volatile&#x5173;&#x952E;&#x5B57;</a></li><li><span class="title-icon "></span><a href="#7-synchronized&#x5173;&#x952E;&#x5B57;"><b></b>7 synchronized&#x5173;&#x952E;&#x5B57;</a></li><li><span class="title-icon "></span><a href="#8-&#x5E76;&#x53D1;&#x4E0B;&#x7684;arraylist"><b></b>8 &#x5E76;&#x53D1;&#x4E0B;&#x7684;ArrayList</a></li></ul></ul></div><a href="#&#x4E8C;&#x3001;java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x4E8C;&#x3001;java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;"><a name="&#x4E8C;&#x3001;java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;&#x3001;java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;" class="plugin-anchor" href="#&#x4E8C;&#x3001;java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;Java&#x5E76;&#x884C;&#x7A0B;&#x5E8F;&#x57FA;&#x7840;</h1>
<h2 id="1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;"><a name="1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;" class="anchor-navigation-ex-anchor" href="#1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;" class="plugin-anchor" href="#1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x65B0;&#x5EFA;&#x7EBF;&#x7A0B;&#xFF1A;</h2>
<pre><code class="lang-java">Thread thread = <span class="hljs-keyword">new</span> Thread();
thread.start();
</code></pre>
<p>&#x8C03;&#x7528;start()&#x65B9;&#x6CD5;&#x540E;&#x81EA;&#x52A8;&#x8C03;&#x7528;run()&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x7EBF;&#x7A0B;&#x65B0;&#x5EFA;&#x65F6;&#x4E0D;&#x80FD;&#x76F4;&#x63A5;&#x8C03;&#x7528;run()&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-java">
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">//&#x65B9;&#x6CD5;&#x4E00; Thread</span>
        Thread thread = <span class="hljs-keyword">new</span> Thread(){
            <span class="hljs-meta">@Override</span>
            <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
                System.out.println(<span class="hljs-string">&quot;run()...&quot;</span>);
            }
        };
        thread.start();
        <span class="hljs-comment">//&#x65B9;&#x6CD5;&#x4E8C; Runnable</span>
        Thread tread1=<span class="hljs-keyword">new</span> Thread(()-&gt;{
            System.out.println(<span class="hljs-string">&quot;run1()...&quot;</span>);
        })
    }
}
</code></pre>
<p>2.2&#x6CE8;&#x610F;&#xFF1A;&#x4E0D;&#x8981;&#x968F;&#x4FBF;&#x4F7F;&#x7528;stop()&#x7ED3;&#x675F;&#x4E00;&#x4E2A;&#x8FDB;&#x7A0B;&#xFF0C;stop()&#x65B9;&#x6CD5;&#x4F1A;&#x76F4;&#x63A5;&#x7EC8;&#x6B62;&#x8FDB;&#x7A0B;&#xFF0C;&#x5E76;&#x91CA;&#x653E;&#x8FD9;&#x4E2A;&#x8FDB;&#x7A0B;&#x6240;&#x6301;&#x6709;&#x7684;&#x9501;&#xFF0C;&#x5BB9;&#x6613;&#x5BFC;&#x81F4;&#x4E0D;&#x540C;&#x7684;&#x7EBF;&#x7A0B;&#x8BFB;&#x9519;&#x6570;&#x636E;&#x3002;</p>
<p>&#x7ED3;&#x675F;&#x8FDB;&#x7A0B;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span></span>{
    <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">boolean</span> stopme = <span class="hljs-keyword">false</span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Stopme</span><span class="hljs-params">()</span></span>{
        stopme = <span class="hljs-keyword">true</span>;
    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
        <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>){
            <span class="hljs-keyword">if</span> (stopme){
                System.out.println(<span class="hljs-string">&quot;exit by stopme&quot;</span>);
                <span class="hljs-keyword">break</span>;
            }
        }
    }
}
</code></pre>
<h2 id="2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;"><a name="2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;" class="plugin-anchor" href="#2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;</h2>
<pre><code class="lang-java">    <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> Thread.interrupt(); <span class="hljs-comment">//&#x4E2D;&#x65AD;&#x8FDB;&#x7A0B;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E2D;&#x65AD;&#x6807;&#x5FD7;&#xFF0C;&#x4F46;&#x4E0D;&#x4F1A;&#x5F3A;&#x5236;&#x4E2D;&#x65AD;</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> Thread.isInterrupted();  <span class="hljs-comment">//&#x68C0;&#x67E5;&#x4E2D;&#x65AD;&#x6807;&#x5FD7;&#x5224;&#x65AD;&#x662F;&#x5426;&#x4E2D;&#x65AD;</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">boolean</span> Thread.interrupt();   <span class="hljs-comment">//&#x5224;&#x65AD;&#x662F;&#x5426;&#x88AB;&#x4E2D;&#x65AD;&#xFF0C;&#x5E76;&#x6E05;&#x9664;&#x5F53;&#x524D;&#x4E2D;&#x65AD;&#x72B6;&#x6001;</span>
</code></pre>
<h2 id="3wait&#x548C;notify"><a name="3wait&#x548C;notify" class="anchor-navigation-ex-anchor" href="#3wait&#x548C;notify"><i class="fa fa-link" aria-hidden="true"></i></a><a name="3wait&#x548C;notify" class="plugin-anchor" href="#3wait&#x548C;notify"><i class="fa fa-link" aria-hidden="true"></i></a>3wait()&#x548C;notify()</h2>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span> </span>{
    <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> Object object = <span class="hljs-keyword">new</span> Object();

<span class="hljs-comment">//    &#x7B49;&#x5F85;</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Thread1</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span></span>{
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
            <span class="hljs-keyword">synchronized</span> (object){
                System.out.println(System.currentTimeMillis()+<span class="hljs-string">&quot;:T1 start!&quot;</span>);
                <span class="hljs-keyword">try</span> {
                    System.out.println(System.currentTimeMillis()+<span class="hljs-string">&quot;:T1 wait for Object!&quot;</span>);
                    object.wait();
                } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis()+<span class="hljs-string">&quot;:T1 end!&quot;</span>);
            }
        }
    }

<span class="hljs-comment">//    &#x5524;&#x9192;&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;</span>
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Thread2</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span></span>{
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
            <span class="hljs-keyword">synchronized</span> (object){
                System.out.println(System.currentTimeMillis()+<span class="hljs-string">&quot;:T2 start! notify one thread&quot;</span>);
                object.notify();
                System.out.println(System.currentTimeMillis()+<span class="hljs-string">&quot;:T2 end!&quot;</span>);
                <span class="hljs-keyword">try</span> {
                    Thread.sleep(<span class="hljs-number">2000</span>);
                } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                }
            }
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        Thread1 thread1 = <span class="hljs-keyword">new</span> Thread1();
        Thread2 thread2 = <span class="hljs-keyword">new</span> Thread2();
        thread1.start();
        thread2.start();
    }
}
</code></pre>
<h2 id="4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;"><a name="4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;" class="anchor-navigation-ex-anchor" href="#4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;" class="plugin-anchor" href="#4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>4&#x6302;&#x8D77;&#xFF08;suspend&#xFF09;&#x548C;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF08;resume&#xFF09;</h2>
<p>&#x4E0D;&#x63A8;&#x8350;&#x4F7F;&#x7528;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;</p>
<h2 id="5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;"><a name="5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;" class="anchor-navigation-ex-anchor" href="#5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;" class="plugin-anchor" href="#5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;"><i class="fa fa-link" aria-hidden="true"></i></a>5&#x7B49;&#x5F85;&#x7EBF;&#x7A0B;&#x7ED3;&#x675F;&#xFF08;join&#xFF09;&#x548C;&#x8C26;&#x8BA9;&#xFF08;yield&#xFF09;</h2>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span> </span>{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">volatile</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>;

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddThread</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Thread</span> </span>{
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10000000</span>; i++);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{
        AddThread addThread = <span class="hljs-keyword">new</span> AddThread();
        addThread.start();
        addThread.join();
        System.out.println(i);
    }
}
</code></pre>
<p><img src="https://img-blog.csdn.net/20180514135019301?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMzMjgzNjUy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="img"></p>
<p>&#x6CE8;&#x91CA;join()&#x65B9;&#x6CD5;&#x540E;&#xFF0C;i&#x8FD4;&#x56DE;&#x4E3A;0&#x3002;</p>
<p>yield()&#x4F1A;&#x4F7F;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x8BA9;&#x51FA;cpu&#xFF0C;&#x91CD;&#x65B0;&#x53C2;&#x4E0E;cpu&#x8D44;&#x6E90;&#x7684;&#x4E89;&#x593A;&#x3002;</p>
<h2 id="6-volatile&#x5173;&#x952E;&#x5B57;"><a name="6-volatile&#x5173;&#x952E;&#x5B57;" class="anchor-navigation-ex-anchor" href="#6-volatile&#x5173;&#x952E;&#x5B57;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="6-volatile&#x5173;&#x952E;&#x5B57;" class="plugin-anchor" href="#6-volatile&#x5173;&#x952E;&#x5B57;"><i class="fa fa-link" aria-hidden="true"></i></a>6 volatile&#x5173;&#x952E;&#x5B57;</h2>
<p>&#x7528;volatile&#x7533;&#x660E;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x540E;&#xFF0C;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x88AB;&#x4FEE;&#x6539;&#x540E;&#x4F1A;&#x901A;&#x77E5;&#x5176;&#x4ED6;&#x7684;&#x7EBF;&#x7A0B;&#x3002;</p>
<h2 id="7-synchronized&#x5173;&#x952E;&#x5B57;"><a name="7-synchronized&#x5173;&#x952E;&#x5B57;" class="anchor-navigation-ex-anchor" href="#7-synchronized&#x5173;&#x952E;&#x5B57;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="7-synchronized&#x5173;&#x952E;&#x5B57;" class="plugin-anchor" href="#7-synchronized&#x5173;&#x952E;&#x5B57;"><i class="fa fa-link" aria-hidden="true"></i></a>7 synchronized&#x5173;&#x952E;&#x5B57;</h2>
<p>&#x5B9E;&#x73B0;&#x7EBF;&#x7A0B;&#x95F4;&#x7684;&#x540C;&#x6B65;&#xFF0C;&#x5BF9;&#x540C;&#x6B65;&#x7684;&#x4EE3;&#x7801;&#x52A0;&#x9501;&#xFF0C;&#x4F7F;&#x5F97;&#x6BCF;&#x4E00;&#x6B21;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8FDB;&#x5165;&#x540C;&#x6B65;&#x5757;&#xFF0C;&#x4ECE;&#x800C;&#x4FDD;&#x8BC1;&#x7EBF;&#x7A0B;&#x95F4;&#x7684;&#x5B89;&#x5168;&#x6027;&#x3002;</p>
<p><strong>&#x7B80;&#x5355;&#x4ECB;&#x7ECD;</strong></p>
<p>synchronized&#x662F;Java&#x4E2D;&#x7684;&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x662F;&#x4E00;&#x79CD;&#x540C;&#x6B65;&#x9501;&#x3002;&#x5B83;&#x4FEE;&#x9970;&#x7684;&#x5BF9;&#x8C61;&#x6709;&#x4EE5;&#x4E0B;&#x51E0;&#x79CD;&#xFF1A; </p>
<p>&#x3000;&#x3000;1. &#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5757;&#xFF0C;&#x88AB;&#x4FEE;&#x9970;&#x7684;&#x4EE3;&#x7801;&#x5757;&#x79F0;&#x4E3A;&#x540C;&#x6B65;&#x8BED;&#x53E5;&#x5757;&#xFF0C;&#x5176;&#x4F5C;&#x7528;&#x7684;&#x8303;&#x56F4;&#x662F;&#x5927;&#x62EC;&#x53F7;{}&#x62EC;&#x8D77;&#x6765;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x4F5C;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x4EE3;&#x7801;&#x5757;&#x7684;&#x5BF9;&#x8C61;&#xFF1B; 
&#x3000;&#x3000;2. &#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x88AB;&#x4FEE;&#x9970;&#x7684;&#x65B9;&#x6CD5;&#x79F0;&#x4E3A;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#xFF0C;&#x5176;&#x4F5C;&#x7528;&#x7684;&#x8303;&#x56F4;&#x662F;&#x6574;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x4F5C;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x8C03;&#x7528;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x5BF9;&#x8C61;&#xFF1B; 
&#x3000;&#x3000;3. &#x4FEE;&#x6539;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5176;&#x4F5C;&#x7528;&#x7684;&#x8303;&#x56F4;&#x662F;&#x6574;&#x4E2A;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF0C;&#x4F5C;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x8FD9;&#x4E2A;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x5BF9;&#x8C61;&#xFF1B; 
&#x3000;&#x3000;4. &#x4FEE;&#x6539;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x5176;&#x4F5C;&#x7528;&#x7684;&#x8303;&#x56F4;&#x662F;synchronized&#x540E;&#x9762;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x4F5C;&#x7528;&#x4E3B;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x8FD9;&#x4E2A;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x5BF9;&#x8C61;&#x3002;</p>
<ul>
<li><strong>&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5757;</strong></li>
</ul>
<p>&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x8BBF;&#x95EE;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;synchronized(this)&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x5757;&#x65F6;&#xFF0C;&#x5176;&#x4ED6;&#x8BD5;&#x56FE;&#x8BBF;&#x95EE;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x7EBF;&#x7A0B;&#x5C06;&#x88AB;&#x963B;&#x585E;&#x3002;</p>
<p>&#x3000;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code>package com.wbg;

import java.util.ArrayList;
import java.util.List;

public class kt {
    public static void main(String[] args) {
        System.out.println(&quot;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;synchronized&quot;);
        SyncThread syncThread = new SyncThread();
        Thread thread1 = new Thread(syncThread, &quot;SyncThread1&quot;);
        Thread thread2 = new Thread(syncThread, &quot;SyncThread2&quot;);
        thread1.start();
        thread2.start();
    }
}
class SyncThread implements Runnable {
    private static int count;
    public SyncThread() {
        count = 0;
    }
    public  void run() {
       synchronized (this){
            for (int i = 0; i &lt; 5; i++) {
                try {
                    System.out.println(&quot;&#x7EBF;&#x7A0B;&#x540D;:&quot;+Thread.currentThread().getName() + &quot;:&quot; + (count++));
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public int getCount() {
        return count;
    }
}
</code></pre><p><strong>&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;synchronized&#x8FD0;&#x884C;&#x7ED3;&#x679C;</strong> </p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211214441.png" alt="image-20201211214441080"></p>
<p><strong>&#x4E0D;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;synchronized&#x8FD0;&#x884C;&#x7ED3;&#x679C;</strong></p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211214451.png" alt="image-20201211214451504"></p>
<p>&#x5F53;&#x4E24;&#x4E2A;&#x5E76;&#x53D1;&#x7EBF;&#x7A0B;(thread1&#x548C;thread2)&#x8BBF;&#x95EE;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;(syncThread)&#x4E2D;&#x7684;synchronized&#x4EE3;&#x7801;&#x5757;&#x65F6;&#xFF0C;&#x5728;&#x540C;&#x4E00;&#x65F6;&#x523B;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x5F97;&#x5230;&#x6267;&#x884C;&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x53D7;&#x963B;&#x585E;&#xFF0C;&#x5FC5;&#x987B;&#x7B49;&#x5F85;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x6267;&#x884C;&#x5B8C;&#x8FD9;&#x4E2A;&#x4EE3;&#x7801;&#x5757;&#x4EE5;&#x540E;&#x624D;&#x80FD;&#x6267;&#x884C;&#x8BE5;&#x4EE3;&#x7801;&#x5757;&#x3002;Thread1&#x548C;thread2&#x662F;&#x4E92;&#x65A5;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x6267;&#x884C;synchronized&#x4EE3;&#x7801;&#x5757;&#x65F6;&#x4F1A;&#x9501;&#x5B9A;&#x5F53;&#x524D;&#x7684;&#x5BF9;&#x8C61;&#xFF0C;&#x53EA;&#x6709;&#x6267;&#x884C;&#x5B8C;&#x8BE5;&#x4EE3;&#x7801;&#x5757;&#x624D;&#x80FD;&#x91CA;&#x653E;&#x8BE5;&#x5BF9;&#x8C61;&#x9501;&#xFF0C;&#x4E0B;&#x4E00;&#x4E2A;&#x7EBF;&#x7A0B;&#x624D;&#x80FD;&#x6267;&#x884C;&#x5E76;&#x9501;&#x5B9A;&#x8BE5;&#x5BF9;&#x8C61;&#x3002; 
&#x6211;&#x4EEC;&#x518D;&#x628A;SyncThread&#x7684;&#x8C03;&#x7528;&#x7A0D;&#x5FAE;&#x6539;&#x4E00;&#x4E0B;&#xFF1A;</p>
<pre><code>public static void main(String[] args) {
        System.out.println(&quot;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;synchronized&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x8FDB;&#x884C;new SyncThread()&quot;);
        Thread thread1 = new Thread( new SyncThread(), &quot;SyncThread1&quot;);
        Thread thread2 = new Thread( new SyncThread(), &quot;SyncThread2&quot;);
        thread1.start();
        thread2.start();
    }
</code></pre><p><strong>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</strong></p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211214603.png" alt="image-20201211214603330"></p>
<p>&#x4E3A;&#x4EC0;&#x4E48;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;thread1&#x548C;thread2&#x540C;&#x65F6;&#x5728;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;synchronized&#x53EA;&#x9501;&#x5B9A;&#x5BF9;&#x8C61;&#xFF0C;&#x6BCF;&#x4E2A;&#x5BF9;&#x8C61;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x9501;&#xFF08;lock&#xFF09;&#x4E0E;&#x4E4B;&#x76F8;&#x5173;&#x8054;&#xFF0C;&#x800C;&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x7B49;&#x540C;&#x4E8E;&#x4E0B;&#x9762;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code>public static void main(String[] args) {
        System.out.println(&quot;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;synchronized&#x6BCF;&#x6B21;&#x8C03;&#x7528;&#x8FDB;&#x884C;new SyncThread()&quot;);
        SyncThread syncThread1 = new SyncThread();
        SyncThread syncThread2 = new SyncThread();
        Thread thread1 = new Thread(syncThread1, &quot;SyncThread1&quot;);
        Thread thread2 = new Thread(syncThread2, &quot;SyncThread2&quot;);
        thread1.start();
        thread2.start();
    }
</code></pre><p> &#x8FD9;&#x65F6;&#x521B;&#x5EFA;&#x4E86;&#x4E24;&#x4E2A;SyncThread&#x7684;&#x5BF9;&#x8C61;syncThread1&#x548C;syncThread2&#xFF0C;&#x7EBF;&#x7A0B;thread1&#x6267;&#x884C;&#x7684;&#x662F;syncThread1&#x5BF9;&#x8C61;&#x4E2D;&#x7684;synchronized&#x4EE3;&#x7801;(run)&#xFF0C;&#x800C;&#x7EBF;&#x7A0B;thread2&#x6267;&#x884C;&#x7684;&#x662F;syncThread2&#x5BF9;&#x8C61;&#x4E2D;&#x7684;synchronized&#x4EE3;&#x7801;(run)&#xFF1B;&#x6211;&#x4EEC;&#x77E5;&#x9053;synchronized&#x9501;&#x5B9A;&#x7684;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD9;&#x65F6;&#x4F1A;&#x6709;&#x4E24;&#x628A;&#x9501;&#x5206;&#x522B;&#x9501;&#x5B9A;syncThread1&#x5BF9;&#x8C61;&#x548C;syncThread2&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x8FD9;&#x4E24;&#x628A;&#x9501;&#x662F;&#x4E92;&#x4E0D;&#x5E72;&#x6270;&#x7684;&#xFF0C;&#x4E0D;&#x5F62;&#x6210;&#x4E92;&#x65A5;&#xFF0C;&#x6240;&#x4EE5;&#x4E24;&#x4E2A;&#x7EBF;&#x7A0B;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x6267;&#x884C;&#x3002;</p>
<ul>
<li><strong>&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;</strong></li>
</ul>
<p>Synchronized&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x5C31;&#x662F;&#x5728;&#x65B9;&#x6CD5;&#x7684;&#x524D;&#x9762;&#x52A0;synchronized&#xFF0C;public synchronized void method(){}; synchronized&#x4FEE;&#x9970;&#x65B9;&#x6CD5;&#x548C;&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5757;&#x7C7B;&#x4F3C;&#xFF0C;&#x53EA;&#x662F;&#x4F5C;&#x7528;&#x8303;&#x56F4;&#x4E0D;&#x4E00;&#x6837;&#xFF0C;&#x4FEE;&#x9970;&#x4EE3;&#x7801;&#x5757;&#x662F;&#x5927;&#x62EC;&#x53F7;&#x62EC;&#x8D77;&#x6765;&#x7684;&#x8303;&#x56F4;&#xFF0C;&#x800C;&#x4FEE;&#x9970;&#x65B9;&#x6CD5;&#x8303;&#x56F4;&#x662F;&#x6574;&#x4E2A;&#x51FD;&#x6570;&#x3002;&#x5982;&#x5C06;&#x7684;run&#x65B9;&#x6CD5;&#x6539;&#x6210;&#x5982;&#x4E0B;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x5B9E;&#x73B0;&#x7684;&#x6548;&#x679C;&#x4E00;&#x6837;&#x3002;</p>
<pre><code> public synchronized void run() {
       {
            for (int i = 0; i &lt; 5; i++) {
                try {
                    System.out.println(&quot;&#x7EBF;&#x7A0B;&#x540D;:&quot;+Thread.currentThread().getName() + &quot;:&quot; + (count++));
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
</code></pre><p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211214832.png" alt="image-20201211214832121"></p>
<ul>
<li><strong>Synchronized&#x4F5C;&#x7528;&#x4E8E;&#x6574;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x5199;&#x6CD5;&#x3002;</strong> </li>
</ul>
<p>&#x5199;&#x6CD5;&#x4E00;:</p>
<pre><code>public synchronized void method()
{
   // todo
}
</code></pre><p>&#x5199;&#x6CD5;&#x4E8C;:</p>
<pre><code>public void method()
{
   synchronized(this) {

   }
}
</code></pre><p>&#x5199;&#x6CD5;&#x4E00;&#x4FEE;&#x9970;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x5199;&#x6CD5;&#x4E8C;&#x4FEE;&#x9970;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x4EE3;&#x7801;&#x5757;&#xFF0C;&#x4F46;&#x5199;&#x6CD5;&#x4E00;&#x4E0E;&#x5199;&#x6CD5;&#x4E8C;&#x662F;&#x7B49;&#x4EF7;&#x7684;&#xFF0C;&#x90FD;&#x662F;&#x9501;&#x5B9A;&#x4E86;&#x6574;&#x4E2A;&#x65B9;&#x6CD5;&#x65F6;&#x7684;&#x5185;&#x5BB9;&#x3002;</p>
<p>&#x5728;&#x7528;synchronized&#x4FEE;&#x9970;&#x65B9;&#x6CD5;&#x65F6;&#x8981;&#x6CE8;&#x610F;&#x4EE5;&#x4E0B;&#x51E0;&#x70B9;&#xFF1A; 
\1. synchronized&#x5173;&#x952E;&#x5B57;&#x4E0D;&#x80FD;&#x7EE7;&#x627F;&#x3002; 
&#x867D;&#x7136;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;synchronized&#x6765;&#x5B9A;&#x4E49;&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;synchronized&#x5E76;&#x4E0D;&#x5C5E;&#x4E8E;&#x65B9;&#x6CD5;&#x5B9A;&#x4E49;&#x7684;&#x4E00;&#x90E8;&#x5206;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;synchronized&#x5173;&#x952E;&#x5B57;&#x4E0D;&#x80FD;&#x88AB;&#x7EE7;&#x627F;&#x3002;&#x5982;&#x679C;&#x5728;&#x7236;&#x7C7B;&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#x65B9;&#x6CD5;&#x4F7F;&#x7528;&#x4E86;synchronized&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x800C;&#x5728;&#x5B50;&#x7C7B;&#x4E2D;&#x8986;&#x76D6;&#x4E86;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x5728;&#x5B50;&#x7C7B;&#x4E2D;&#x7684;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x5E76;&#x4E0D;&#x662F;&#x540C;&#x6B65;&#x7684;&#xFF0C;&#x800C;&#x5FC5;&#x987B;&#x663E;&#x5F0F;&#x5730;&#x5728;&#x5B50;&#x7C7B;&#x7684;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4E2D;&#x52A0;&#x4E0A;synchronized&#x5173;&#x952E;&#x5B57;&#x624D;&#x53EF;&#x4EE5;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x5728;&#x5B50;&#x7C7B;&#x65B9;&#x6CD5;&#x4E2D;&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x4E2D;&#x76F8;&#x5E94;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x8FD9;&#x6837;&#x867D;&#x7136;&#x5B50;&#x7C7B;&#x4E2D;&#x7684;&#x65B9;&#x6CD5;&#x4E0D;&#x662F;&#x540C;&#x6B65;&#x7684;&#xFF0C;&#x4F46;&#x5B50;&#x7C7B;&#x8C03;&#x7528;&#x4E86;&#x7236;&#x7C7B;&#x7684;&#x540C;&#x6B65;&#x65B9;&#x6CD5;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x5B50;&#x7C7B;&#x7684;&#x65B9;&#x6CD5;&#x4E5F;&#x5C31;&#x76F8;&#x5F53;&#x4E8E;&#x540C;&#x6B65;&#x4E86;&#x3002;&#x8FD9;&#x4E24;&#x79CD;&#x65B9;&#x5F0F;&#x7684;&#x4F8B;&#x5B50;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A; 
&#x5728;&#x5B50;&#x7C7B;&#x65B9;&#x6CD5;&#x4E2D;&#x52A0;&#x4E0A;synchronized&#x5173;&#x952E;&#x5B57;</p>
<pre><code>lass Parent {
   public synchronized void method() { }
}
class Child extends Parent {
   public synchronized void method() { }
}
</code></pre><p>&#x5728;&#x5B50;&#x7C7B;&#x65B9;&#x6CD5;&#x4E2D;&#x8C03;&#x7528;&#x7236;&#x7C7B;&#x7684;&#x540C;&#x6B65;&#x65B9;&#x6CD5;</p>
<pre><code>class Parent {
   public synchronized void method() {   }
}
class Child extends Parent {
   public void method() { super.method();   }
}
</code></pre><ul>
<li><strong>&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x7684;&#x65B9;&#x6CD5;</strong></li>
</ul>
<p>Synchronized&#x4E5F;&#x53EF;&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF0C;&#x7528;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>public synchronized static void method() {

}
</code></pre><p>&#x6211;&#x4EEC;&#x77E5;&#x9053;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x662F;&#x5C5E;&#x4E8E;&#x7C7B;&#x7684;&#x800C;&#x4E0D;&#x5C5E;&#x4E8E;&#x5BF9;&#x8C61;&#x7684;&#x3002;&#x540C;&#x6837;&#x7684;&#xFF0C;synchronized&#x4FEE;&#x9970;&#x7684;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x9501;&#x5B9A;&#x7684;&#x662F;&#x8FD9;&#x4E2A;&#x7C7B;&#x7684;&#x6240;&#x6709;&#x5BF9;&#x8C61;&#x3002;</p>
<p><strong>synchronized&#x4FEE;&#x9970;&#x9759;&#x6001;&#x65B9;&#x6CD5;</strong></p>
<pre><code>package com.wbg;

import java.util.ArrayList;
import java.util.List;

public class kt {
    public static void main(String[] args) {
        System.out.println(&quot;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;&#x9759;&#x6001;synchronized&quot;);
        SyncThread syncThread = new SyncThread();
        Thread thread1 = new Thread(syncThread, &quot;SyncThread1&quot;);
        Thread thread2 = new Thread(syncThread, &quot;SyncThread2&quot;);
        thread1.start();
        thread2.start();
    }
}
class SyncThread implements Runnable {
    private static int count;

    public SyncThread() {
        count = 0;
    }

    public synchronized static void method() {
        for (int i = 0; i &lt; 5; i ++) {
            try {
                System.out.println(Thread.currentThread().getName() + &quot;:&quot; + (count++));
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void run() {
        method();
    }
}
</code></pre><p><strong>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</strong></p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211215032.png" alt="image-20201211215032638"></p>
<p>syncThread1&#x548C;syncThread2&#x662F;SyncThread&#x7684;&#x4E24;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x4F46;&#x5728;thread1&#x548C;thread2&#x5E76;&#x53D1;&#x6267;&#x884C;&#x65F6;&#x5374;&#x4FDD;&#x6301;&#x4E86;&#x7EBF;&#x7A0B;&#x540C;&#x6B65;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;run&#x4E2D;&#x8C03;&#x7528;&#x4E86;&#x9759;&#x6001;&#x65B9;&#x6CD5;method&#xFF0C;&#x800C;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x662F;&#x5C5E;&#x4E8E;&#x7C7B;&#x7684;&#xFF0C;&#x6240;&#x4EE5;syncThread1&#x548C;syncThread2&#x76F8;&#x5F53;&#x4E8E;&#x7528;&#x4E86;&#x540C;&#x4E00;&#x628A;&#x9501;&#x3002;&#x8FD9;&#x4E0E;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;synchronized&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x76F8;&#x540C;</p>
<ul>
<li><strong>&#x4FEE;&#x9970;&#x4E00;&#x4E2A;&#x7C7B;</strong></li>
</ul>
<p>Synchronized&#x8FD8;&#x53EF;&#x4F5C;&#x7528;&#x4E8E;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x7528;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<p><strong>&#x8FD0;&#x884C;&#x7ED3;&#x679C;</strong></p>
<p><img src="https://images2018.cnblogs.com/blog/1418466/201808/1418466-20180813224557391-772452008.png" alt="img"></p>
<p>&#x6548;&#x679C;&#x548C;&#x4E0A;&#x9762;synchronized&#x4FEE;&#x9970;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;synchronized&#x4F5C;&#x7528;&#x4E8E;&#x4E00;&#x4E2A;&#x7C7B;T&#x65F6;&#xFF0C;&#x662F;&#x7ED9;&#x8FD9;&#x4E2A;&#x7C7B;T&#x52A0;&#x9501;&#xFF0C;T&#x7684;&#x6240;&#x6709;&#x5BF9;&#x8C61;&#x7528;&#x7684;&#x662F;&#x540C;&#x4E00;&#x628A;&#x9501;&#x3002;</p>
<p><strong>&#x603B;&#x7ED3;:</strong></p>
<p>1&#x3001; &#x65E0;&#x8BBA;synchronized&#x5173;&#x952E;&#x5B57;&#x52A0;&#x5728;&#x65B9;&#x6CD5;&#x4E0A;&#x8FD8;&#x662F;&#x5BF9;&#x8C61;&#x4E0A;&#xFF0C;&#x5982;&#x679C;&#x5B83;&#x4F5C;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x975E;&#x9759;&#x6001;&#x7684;&#xFF0C;&#x5219;&#x5B83;&#x53D6;&#x5F97;&#x7684;&#x9501;&#x662F;&#x5BF9;&#x8C61;&#xFF1B;&#x5982;&#x679C;synchronized&#x4F5C;&#x7528;&#x7684;&#x5BF9;&#x8C61;&#x662F;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x6216;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x5219;&#x5B83;&#x53D6;&#x5F97;&#x7684;&#x9501;&#x662F;&#x5BF9;&#x7C7B;&#xFF0C;&#x8BE5;&#x7C7B;&#x6240;&#x6709;&#x7684;&#x5BF9;&#x8C61;&#x540C;&#x4E00;&#x628A;&#x9501;&#x3002; 
2&#x3001;&#x6BCF;&#x4E2A;&#x5BF9;&#x8C61;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x9501;&#xFF08;lock&#xFF09;&#x4E0E;&#x4E4B;&#x76F8;&#x5173;&#x8054;&#xFF0C;&#x8C01;&#x62FF;&#x5230;&#x8FD9;&#x4E2A;&#x9501;&#x8C01;&#x5C31;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#x5B83;&#x6240;&#x63A7;&#x5236;&#x7684;&#x90A3;&#x6BB5;&#x4EE3;&#x7801;&#x3002; 
3&#x3001;&#x5B9E;&#x73B0;&#x540C;&#x6B65;&#x662F;&#x8981;&#x5F88;&#x5927;&#x7684;&#x7CFB;&#x7EDF;&#x5F00;&#x9500;&#x4F5C;&#x4E3A;&#x4EE3;&#x4EF7;&#x7684;&#xFF0C;&#x751A;&#x81F3;&#x53EF;&#x80FD;&#x9020;&#x6210;&#x6B7B;&#x9501;&#xFF0C;&#x6240;&#x4EE5;&#x5C3D;&#x91CF;&#x907F;&#x514D;&#x65E0;&#x8C13;&#x7684;&#x540C;&#x6B65;&#x63A7;&#x5236;</p>
<h2 id="8-&#x5E76;&#x53D1;&#x4E0B;&#x7684;arraylist"><a name="8-&#x5E76;&#x53D1;&#x4E0B;&#x7684;arraylist" class="anchor-navigation-ex-anchor" href="#8-&#x5E76;&#x53D1;&#x4E0B;&#x7684;arraylist"><i class="fa fa-link" aria-hidden="true"></i></a><a name="8-&#x5E76;&#x53D1;&#x4E0B;&#x7684;arraylist" class="plugin-anchor" href="#8-&#x5E76;&#x53D1;&#x4E0B;&#x7684;arraylist"><i class="fa fa-link" aria-hidden="true"></i></a>8 &#x5E76;&#x53D1;&#x4E0B;&#x7684;ArrayList</h2>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Client</span> </span>{
    <span class="hljs-keyword">static</span> ArrayList&lt;Integer&gt; arrayList = <span class="hljs-keyword">new</span> ArrayList&lt;Integer&gt;(<span class="hljs-number">10</span>);

    <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AddThread</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Runnable</span></span>{
        <span class="hljs-meta">@Override</span>
        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span></span>{
            <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000000</span>; i++) {
                arrayList.add(i);
            }
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> InterruptedException</span>{
        Thread thread1 = <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> AddThread());
        Thread thread2 = <span class="hljs-keyword">new</span> Thread(<span class="hljs-keyword">new</span> AddThread());
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        System.out.println(arrayList.size());
    }
}
</code></pre>
<p>&#x7ED9;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x4F1A;&#x6709;&#x4E09;&#x79CD;&#x7ED3;&#x679C;</p>
<p>1&#xFF09;&#x7A0B;&#x5E8F;&#x8D8A;&#x754C;:</p>
<p><img src="https://gitee.com/zisuu/picture/raw/master/img/20201211213710.png" alt="image-20201211213710310"></p>
<p>2&#xFF09;&#x591A;&#x7EBF;&#x7A0B;&#x51B2;&#x7A81;&#xFF0C;&#x5BFC;&#x81F4;&#x7ED3;&#x679C;&#x53D8;&#x52A8;</p>
<p>3&#xFF09;&#x6781;&#x5C11;&#x6570;&#x60C5;&#x51B5;&#x4F1A;&#x51FA;&#x73B0;&#x6B63;&#x786E;&#x7ED3;&#x679C;</p>
<p>&#x6539;&#x8FDB;&#x65B9;&#x6CD5;&#xFF1A;&#x7528;Vector&#x4EE3;&#x66FF;ArrayList</p>
<pre><code>static Vector&lt;Integer&gt; arrayList = new Vector&lt;Integer&gt;(10);
</code></pre><p>2.10&#x5E76;&#x53D1;&#x4E0B;&#x7684;hashmap</p>
<p>&#x540C;ArrayList&#x95EE;&#x9898;&#xFF0C;&#x6240;&#x4EE5;&#x5EFA;&#x8BAE;&#x5E76;&#x53D1;&#x4E0B;&#x7528;ConcurrentHashMap&#x4EE3;&#x66FF;HashMap</p>

<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="basic/0.java内存模型与线程.html" class="navigation navigation-prev " aria-label="Previous page: 2.java内存模型与线程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="basic/0.jhappenBefore与jvm指令重排.html" class="navigation navigation-next " aria-label="Next page: 3.happenBefore与jvm指令重排序">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.并行程序基础","level":"6.3","depth":1,"next":{"title":"3.happenBefore与jvm指令重排序","level":"6.4","depth":1,"path":"多线程/basic/0.jhappenBefore与jvm指令重排.md","ref":"多线程/basic/0.jhappenBefore与jvm指令重排.md","articles":[]},"previous":{"title":"2.java内存模型与线程","level":"6.2","depth":1,"path":"多线程/basic/0.java内存模型与线程.md","ref":"多线程/basic/0.java内存模型与线程.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","sharing-plus","anchor-navigation-ex","favicon","expandable-chapters","toggle-chapters","back-to-top-button","code","-lunr","-search","search-pro","pageview-count","popup","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"livereload":{},"splitter":{},"search-pro":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"popup":{},"code":{"copyButtons":true},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{},"back-to-top-button":{},"pageview-count":{},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"toggle-chapters":{},"expandable-chapters":{}},"theme":"default","author":"hzh","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"计算机 ,java , 大数据 基础知识体系","language":"zh-hans","gitbook":"3.2.3"},"file":{"path":"多线程/3.并行程序基础.md","mtime":"2020-12-18T13:41:27.835Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-30T11:12:56.611Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

